package com.example.spring_config.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring_config.anno.PermissionVerAnno;
import com.example.spring_config.bean.ResultInfo;
import com.example.spring_config.mapper.Permission;
import com.example.spring_config.mapper.User;
import com.example.spring_config.service.PermissionService;
import com.example.spring_config.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    /**
     * 获取用户列表
     * @return
     */
    @PermissionVerAnno("getUserList")
    @RequestMapping("/getUserList")
    public Object getUserList(Integer page,Integer limit){
        Page<User> objectPage = new Page<>();
        objectPage.setCurrent(page);
        objectPage.setSize(limit);
        Page<User> page1 = userService.page(objectPage);
        List<User> list = userService.getUserRole(page1.getRecords());
        //List<User> list = userService.getUserList();
        if(list.size()==0){
            return ResultInfo.no("获取用户列表失败");
        }else {
            return list;
        }
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PermissionVerAnno("addUser")
    @RequestMapping("/addUser")
    public ResultInfo addUser(User user){
        boolean save = userService.save(user);
        if(save){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("用户添加失败");
        }
    }

    /**
     * 根据用户id获取用户
     * @param id
     * @return
     */
    @PermissionVerAnno("getUserById")
    @RequestMapping("/getUserById")
    public ResultInfo getUserById(String id){
        User byId = userService.getById(id);
        if(byId==null){
            return ResultInfo.no("未查询到此用户");
        }else {
            return ResultInfo.ok(byId);
        }
    }

    /**
     * 根据用户id修改用户
     * @param user 用户
     * @param roleList 用户角色id集合
     * @return
     */
    @PermissionVerAnno("updateUser")
    @RequestMapping("/updateUser")
    public ResultInfo updateUser(User user,String roleList){
        Boolean aBoolean = userService.updateUser(user, roleList);
        if(aBoolean==null){
            return ResultInfo.no("用户修改失败");
        }else {
            return ResultInfo.ok();
        }
    }

    /**
     * 根据用户id删除用户
     * @param id
     * @return
     */
    @PermissionVerAnno("deleteUserById")
    @RequestMapping("/deleteUserById")
    public ResultInfo deleteUserById(String id){
        System.out.println("删除的用户id："+id);
        boolean b = userService.removeById(id);
        if(b){
            return ResultInfo.ok();
        }else {
            return ResultInfo.no("用户删除失败");
        }
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @RequestMapping("/loginUser")
    public ResultInfo login(User user, HttpSession session){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        User one = userService.getOne(queryWrapper);
        if(one==null){
            return ResultInfo.no("用户名或密码错误");
        }else {
            // 把用户存入session
            session.setAttribute("login_user",one);
            // 获取所有的权限
            List<Permission> allPermission = permissionService.list();
            HashMap<String, Permission> allPermissionMap = new HashMap<>();
            allPermission.forEach(t->{
                allPermissionMap.put(t.getId(),t);
            });
            session.setAttribute("allPermissionMap",allPermissionMap);
            // 获取用户所有的权限
            Set<String> userPermission = permissionService.getUserPermissionUrlByRid(one.getId());
            // 把用户权限存入session
            session.setAttribute("userPermission",userPermission);
            return ResultInfo.ok("登录成功",one.getId());
        }
    }

}
